package com.study.contoller;

import com.alibaba.fastjson.JSON;
import com.study.R;
import com.study.dto.Search;
import com.study.entity.Counties;
import com.study.service.CountiesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Path;

/**
 * @author lxl
 * @version 2022-06-20 16:06
 */

@RestController
@RequestMapping("/api/counties")
public class CountiesController {

    private Logger log = LoggerFactory.getLogger(CountiesController.class);


    @Autowired
    private CountiesService countiesService;


    @GetMapping("selectsAll")
    public R selectsAll(){
        return R.ok().setData(countiesService.selectAll());
    }


    /**
     * 添加城区记录
     */
    @PostMapping("insert")
    public R insert(@RequestBody Counties counties) {
        log.debug("前台传递数据:{}", JSON.toJSON(counties));
        return R.ok().setData(countiesService.insert(counties));
    }

    /**
     * 根据主键删除县区记录
     */
    @DeleteMapping("{countiesId}")
    public R deleteByPrimary(@PathVariable Integer countiesId) {
        return R.ok().setData(countiesService.deleteByPrimaryKey(countiesId));
    }

    /**
     * 通过主键修改县区记录
     */
    @PutMapping("update-by-primary")
    public R updateByPrimary(@RequestBody Counties counties) {
        return R.ok().setData(countiesService.updateByPrimaryKey(counties));
    }

    /**
     * 查询所有的县区记录
     */
    @GetMapping("selects")
    public R selects(Search search) {
        return R.ok().setData(countiesService.selects(search));
    }

    /**
     * 通过id查询
     */
    @GetMapping("select-by-primary/{countiesId}")
    public R selectByPrimary(@PathVariable Integer countiesId) {
        return R.ok().setData(countiesService.selectByPrimaryKey(countiesId));
    }

    /**
     * 模糊查询
     */
    @GetMapping("dim-select-by-name/{countiesName}")
    public R dimSelectByName(@PathVariable String countiesName) {
        return R.ok().setData(countiesService.dimByName(countiesName));
    }
}
